package com.example.utils;

/**
 * PTZ控制
 *
 * @author: Fred
 * @email 453086@qq.com
 * @create: 2024-12-02 13:14
 */
public class PTZUtil {

    // 每纬度的长度，单位为米
    private static final double METERS_PER_LATITUDE = 111120;


    /**
     * 计算2个点之间的方向角（Pan）
     */
    public static double calculatePanAngle(double lon1, double lat1, double lon2, double lat2) {

        // 将角度转换为弧度
        double radLat1 = Math.toRadians(lat1);
        double radLon1 = Math.toRadians(lon1);
        double radLat2 = Math.toRadians(lat2);
        double radLon2 = Math.toRadians(lon2);

        // 计算经度差
        double deltaLon = radLon2 - radLon1;

        // 方位角公式
        double y = Math.sin(deltaLon) * Math.cos(radLat2);
        double x = Math.cos(radLat1) * Math.sin(radLat2)
                - Math.sin(radLat1) * Math.cos(radLat2) * Math.cos(deltaLon);
        double panRadians = Math.atan2(y, x);

        // 将弧度转换为角度，并确保在0-360度范围内
        double panDegrees = Math.toDegrees(panRadians);
        return (panDegrees + 360) % 360;
    }

    /**
     * 计算 Tilt 俯仰角
     */
    public static double calculateTiltAngle(double distance, double height) {

        // 计算 Tilt 角度
        double tiltRadians = Math.atan2(height, distance);
        // 将弧度转换为角度
        double tiltDegrees =  Math.toDegrees(tiltRadians);
        return (tiltDegrees + 360) % 360;
    }

}
